package servlet.rool;

import net.sf.json.JSONObject;
import pojo.Role;
import service.AdRossService;
import service.AdminService;
import service.RoleService;
import service.impl.AdRossServiceImpl;
import service.impl.AdminServiceImpl;
import service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 用户批量删除的servlet
 */
@WebServlet(value = "/AdminDeleAllServlet")
public class AdminDeleAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        String id= request.getParameter("str");
        //实例化service层
        AdminService as=new AdminServiceImpl();
        AdRossService ars=new AdRossServiceImpl();

        //定义变量
        int bo=0;
        int bo2=0;
        boolean flag=true;
        //将页面接收到的值进行拆分
        String cheid[]=id.split(",");
        //判断是否包含超级管理员
        for(int i=0;i<cheid.length;i++){
            RoleService rs=new RoleServiceImpl();
            Role role=rs.queryOneRole(Integer.parseInt(cheid[i]));
            if(role.getRoId()!=1) {
                continue;
            }else {
                flag=false;
                break;
            }
        }
        JSONObject json=new JSONObject();
        if(flag){
            //拆分后循环运行方法
            for(int i=0;i<cheid.length;i++){
                    bo = as.deleAdmin(Integer.parseInt(cheid[i]));
                    bo2 = ars.deleUrass(Integer.parseInt(cheid[i]));
            }
            //判断结果
            if(bo>0 && bo2>0){
                json.put("msg","删除成功！");
            }else{
                json.put("msg","删除失败!");
            }
        }else {
            json.put("msg","不可删除最高权限管理员");
        }

        PrintWriter out=response.getWriter();
        out.print(json);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        doGet(request, response);
    }
}
